/**
* @author Alexander Farkas
* v. 1.02
*/

(function($) {
    $.extend($.fx.step,{
        backgroundPosition: function(fx) {
            if (fx.state === 0 && typeof fx.end == 'string') {
                var start = $.curCSS(fx.elem,'backgroundPosition');
                start = toArray(start);
                fx.start = [start[0],start[2]];
                var end = toArray(fx.end);
                fx.end = [end[0],end[2]];
                fx.unit = [end[1],end[3]];
            }
            var nowPosX = [];
            nowPosX[0] = ((fx.end[0] - fx.start[0]) * fx.pos) + fx.start[0] + fx.unit[0];
            nowPosX[1] = ((fx.end[1] - fx.start[1]) * fx.pos) + fx.start[1] + fx.unit[1];
            fx.elem.style.backgroundPosition = nowPosX[0]+' '+nowPosX[1];

            function toArray(strg){
                strg = strg.replace(/left|top/g,'0px');
                strg = strg.replace(/right|bottom/g,'100%');
                strg = strg.replace(/([0-9\.]+)(\s|\)|$)/g,"$1px$2");
                var res = strg.match(/(-?[0-9\.]+)(px|\%|em|pt)\s(-?[0-9\.]+)(px|\%|em|pt)/);
                return [parseFloat(res[1],10),res[2],parseFloat(res[3],10),res[4]];
            }
        }
    });
})(jQuery);

/*Jquery Busy.min*/
(function($){function Busy(a){this.options=$.extend({},Busy.defaults,a)};Busy.instances=[];Busy.repositionAll=function(){for(var i=0;i<Busy.instances.length;i++){if(!Busy.instances[i])continue;var a=Busy.instances[i].options;new Busy(a).positionImg($(Busy.instances[i].target),$.data(Busy.instances[i].target,"busy"),a.position)}};Busy.prototype.hide=function(b){b.each(function(){var a=$.data(this,"busy");if(a)a.remove();$(this).css("visibility","");$.data(this,"busy",null);for(var i=0;i<Busy.instances.length;i++)if(Busy.instances[i]!=null&&Busy.instances[i].target==this)Busy.instances[i]=null})};Busy.prototype.show=function(c){var d=this;c.each(function(){if($.data(this,"busy"))return;var a=$(this);var b=d.buildImg();b.css("visibility","hidden");b.load(function(){d.positionImg(a,b,d.options.position);b.css("visibility","")});$("body").append(b);if(d.options.hide)a.css("visibility","hidden");$.data(this,"busy",b);Busy.instances.push({target:this,options:d.options})})};Busy.prototype.preload=function(){var a=this.buildImg();a.css("visibility","hidden");a.load(function(){$(this).remove()});$("body").append(a)};Busy.prototype.buildImg=function(){var a="<img src='"+this.options.img+"' alt='"+this.options.alt+"' title='"+this.options.title+"'";if(this.options.width)a+=" width='"+this.options.width+"'";if(this.options.height)a+=" height='"+this.options.height+"'";a+=" />";return $(a)};Busy.prototype.positionImg=function(a,b,c){var d=a.offset();var e=a.outerWidth();var f=a.outerHeight();var g=b.outerWidth();var h=b.outerHeight();if(c=="left"){var i=d.left-g-this.options.offset}else if(c=="right"){var i=d.left+e+this.options.offset}else{var i=d.left+(e-g)/2.0}var j=d.top+(f-h)/2.0;b.css("position","absolute");b.css("left",i+"px");b.css("top",j+"px")};Busy.defaults={img:'busy.gif',alt:'Please wait...',title:'Please wait...',hide:true,position:'center',zIndex:1001,width:null,height:null,offset:10};$.fn.busy=function(a,b){if($.inArray(a,["clear","hide","remove"])!=-1){new Busy(a).hide($(this))}else if(a=="defaults"){$.extend(Busy.defaults,b||{})}else if(a=="preload"){new Busy(a).preload()}else if(a=="reposition"){Busy.repositionAll()}else{new Busy(a).show($(this));return $(this)}}})(jQuery);


$(function(){  
    
    base_url_srch = 'http://localhost/busterminal-timetable.me/';
    //base_url_srch = 'http://www.busterminal.me/admin/';
    
    console.log('do')
    
    $.ajax({
            url: base_url_srch+'stanica/core/ajax_ac_complete/',
            type: 'POST',
            success: function(data){
                $("input.ac_stanica").autocomplete({ source: data.html }); 
            },
            dataType: 'json'
        });
    
    
    $('#trazi').live('click', function(){
        
        var that = jQuery(this).busy({ img :base_url_srch+'assets/img/loader/ajax-loader-red.gif'});
        
        /***********************************************************
        * DATEPICKER 1
        ***********************************************************/
        var chosen_date = $("#datumkomponenta1").datepicker("getDate");        
        
        var mesec = chosen_date.getMonth()+1;
        var godina = chosen_date.getFullYear();
                            
        seldate = chosen_date.getDate() + "-" + mesec + "-" + godina;
        //seldate = chosen_date;
        
        $.ajax({
            type: 'GET',
            url: base_url_srch+'search/core/search/?jsoncall=?',
            data: ({                
                srch_polazak : $('#srch_polazak').val(),
                srch_dolazak : $('#srch_dolazak').val(),
                datum: seldate,
                type: timetable_type
                
            }),
            dataType: 'jsonp',
            success: function(data){
              
                that.busy("hide"); 
                
                if(data.success==true){
                    
                    $('#infomessage').hide();
                    
                    if(data.danasnjidan==false){
                        
                        $('#infomessage').html('Za ovaj datum nema polazaka za traženu rutu!');
                        $('#infomessage').show();
                        
                    }
                    
                    $('#infomessage_search').hide();
                    $('#polasci_tbl tbody').html(data.html);
                    if(data.trazenidatum == 'Cetvrtak') data.trazenidatum = 'Četvrtak';
                    $('.vozniRedHead').html(data.trazenidatum+ ' - ' +data.trazenidan);
                    $('#paginator').html(data.paginator);
                    $('.tbl_total').html(data.count);
                    $('#pregled_pretraga').show();
                    
                }else{
                    
                    $('#infomessage_search').show();
                    $('#infomessage_search').html(data.html);
                    
                }  
                             
                $.modal.close();
                
            },
            error:function(data){
                $.modal.close(); 
                alert("Error: " + data);
            }
        });
    })
    
    //$("#trazi").trigger('click');
});